import request from '@/utils/request'
import { MenuParams, PermissionResponseData } from './type'

enum PERMISSION_API_URL {
  /**
   * 获取全部菜单与按钮的标识数据接口的地址
   */
  GET_ALL_PERMISSION_URL = '/admin/acl/permission',

  /**
   * 新增菜单接口的地址
   */
  ADD_MENU_URL = '/admin/acl/permission/save',

  /**
   * 修改菜单接口的地址
   */
  EDIT_MENU_URL = '/admin/acl/permission/update',

  /**
   * 删除菜单接口的地址
   */
  DELETE_MENU_URL = '/admin/acl/permission/remove',
}

/**
 * 获取菜单数据的接口
 * @returns Promise<PermissionResponseData>
 */
export const reqAllPermission = (): Promise<PermissionResponseData> =>
  request.get<any, PermissionResponseData>(PERMISSION_API_URL.GET_ALL_PERMISSION_URL)

/**
 * 添加/修改菜单的接口
 * @param data
 * @returns Promise<any>
 */
export const reqAddOrEditMenu = (data: MenuParams): Promise<any> => {
  if (data.id) {
    return request.put<any, any>(PERMISSION_API_URL.EDIT_MENU_URL, data)
  } else {
    return request.post<any, any>(PERMISSION_API_URL.ADD_MENU_URL, data)
  }
}

/**
 * 删除菜单的接口
 * @param id
 * @returns Promise<any>
 */
export const reqDeleteMenu = (id: number): Promise<any> =>
  request.delete<any, any>(PERMISSION_API_URL.DELETE_MENU_URL + `/${id}`)
